package top.hhydra.mimall.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.hhydra.mimall.constant.MallConst;
import top.hhydra.mimall.entity.User;
import top.hhydra.mimall.form.CartAddForm;
import top.hhydra.mimall.form.CartUpdateForm;
import top.hhydra.mimall.service.ICartService;
import top.hhydra.mimall.vo.CartVo;
import top.hhydra.mimall.vo.ResponseVo;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;

/**
 * @Description
 * @auther SaiyeEmon
 * @create 2020-03-18 0:46
 */
@Api(value="购物车controller",tags={"购物车操作接口"})
public class CartController {
    @Autowired
    private ICartService cartService;

    @GetMapping("/carts")
    @ApiOperation(value = "查询购物车所有商品")
    public ResponseVo<CartVo> list(HttpSession session) {
        User user = (User) session.getAttribute(MallConst.CURRENT_USER);
        return cartService.list(user.getId());
    }

    @PostMapping("/carts")
    @ApiOperation(value = "添加商品到购物车")
    public ResponseVo<CartVo> add(@Valid @RequestBody CartAddForm cartAddForm,
                                  HttpSession session) {
        User user = (User) session.getAttribute(MallConst.CURRENT_USER);
        return cartService.add(user.getId(), cartAddForm);
    }

    @PutMapping("/carts/{productId}")
    @ApiOperation(value = "更新购物车的商品")
    public ResponseVo<CartVo> update(@PathVariable Integer productId,
                                     @Valid @RequestBody CartUpdateForm form,
                                     HttpSession session) {
        User user = (User) session.getAttribute(MallConst.CURRENT_USER);
        return cartService.update(user.getId(), productId, form);
    }

    @DeleteMapping("/carts/{productId}")
    @ApiOperation(value = "删除购物车的商品")
    public ResponseVo<CartVo> delete(@PathVariable Integer productId,
                                     HttpSession session) {
        User user = (User) session.getAttribute(MallConst.CURRENT_USER);
        return cartService.delete(user.getId(), productId);
    }

    @PutMapping("/carts/selectAll")
    @ApiOperation(value = "全选购物车的商品")
    public ResponseVo<CartVo> selectAll(HttpSession session) {
        User user = (User) session.getAttribute(MallConst.CURRENT_USER);
        return cartService.selectAll(user.getId());
    }

    @PutMapping("/carts/unSelectAll")
    @ApiOperation(value = "全不选购物车的商品")
    public ResponseVo<CartVo> unSelectAll(HttpSession session) {
        User user = (User) session.getAttribute(MallConst.CURRENT_USER);
        return cartService.unSelectAll(user.getId());
    }

    @GetMapping("/carts/products/sum")
    @ApiOperation(value = "获取购物车商品总数量")
    public ResponseVo<Integer> sum(HttpSession session) {
        User user = (User) session.getAttribute(MallConst.CURRENT_USER);
        return cartService.sum(user.getId());
    }
}
